﻿using DG.Tweening;
using TMPro;
using UnityEngine.UI;

namespace HotFixPro
{
    public class Login : BasePanel
    {
        //账号密码
        private TMP_InputField _username_Ipt;
        private TMP_InputField _password_Ipt;

        private TextMeshProUGUI _tips_Txt;

        private Button _login_Btn;

        private string _username;
        private string _password;



        protected override void OnInitComponent()
        {
            base.OnInitComponent();
            _username_Ipt = transform.GetComponentFromChild<TMP_InputField>("Username_Ipt");
            _password_Ipt = transform.GetComponentFromChild<TMP_InputField>("Password_Ipt");
            _tips_Txt = transform.GetComponentFromChild<TextMeshProUGUI>("BG/Gradient/Clouds/Content/Tips_Txt");
            _tips_Txt.alpha = 0;
            _login_Btn = transform.FindChildButton("SignIn_Btn");
        }

        protected override void OnAddListener()
        {
            base.OnAddListener();
            _username_Ipt.onEndEdit.AddListener(content =>
            {
                _username = content;
            });
            _password_Ipt.onEndEdit.AddListener(content =>
            {
                _password = content;
            });
            _login_Btn.OnRegister(CheckAccount);
        }
        protected override void OnRemoveListener()
        {
            base.OnRemoveListener();
            _username_Ipt.onEndEdit.RemoveAllListeners();
            _password_Ipt.onEndEdit.RemoveAllListeners();
            _login_Btn.OnClear();
        }

        //检测账户
        public void CheckAccount()
        {
            if (string.IsNullOrEmpty(_username) || string.IsNullOrEmpty(_password))
            {
                TipsCtrl("账户或密码不能为空哦");
                return;
            }
            if (_username != "ye" || _password != "zhu")
            {
                TipsCtrl("账户或密码不正确");
                return;
            }
            Close(true);
            UIMgr.Instance.OpenPanelAsync("Hall");
        }

        private void TipsCtrl(string value)
        {
            _tips_Txt.DOKill();
            _tips_Txt.SetText(value);
            _tips_Txt.DOFade(1, 1).OnComplete(() =>
            {
                _tips_Txt.DOFade(0, 1);
            });
        }

    }
}